extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;


mod square;
mod geometry;
mod console;


use self::square::square::Square;
use self::square::square::Shape;

use wasm_bindgen::JsCast;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console, js_name = log)]
    fn log(s: &str);
}

macro_rules! console_log_rs {
    ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}


struct World {
    data: [[bool; 10]; 20]
}

#[wasm_bindgen]
struct GameState {
    square: Square,
    next_square: Square
}

#[wasm_bindgen]
impl GameState {

    pub fn new(idx: i32) -> GameState{
        let shape = Shape::new();
        let next_idx = (idx + 1) % 6;
        GameState { square: Square::new(shape, idx), next_square: Square::new(shape, next_idx) }
    }

    pub fn init(&self) {
        self.square.draw();
    }

    pub fn key_up(&mut self) {
        self.square.key_up();
    }

    pub fn key_left(&mut self) {
        self.square.key_left();
    }

    pub fn key_right(&mut self) {
        self.square.key_right();
    }

    pub fn game_loop(&mut self, idx: i32) -> bool {
        if !self.square.update_state_sucess() {
            if self.square.game_over() {
                return false;
            }
            self.square.eliminate_if_need();
            self.square = self.next_square;
            let shape = Shape::new();
            self.next_square = Square::new(shape, idx);
        } else {
        }
        return true;
    }

}